
MyDialog={}
--[[
function MyDialog.EditDialog(init)
  local function initTable(name)
    if init[name]==nil then init[name]={} end
  end
  initTable("onClick")
  initTable("id")
  _ENV[init.id.Dia or "dia"]=AlertDialog.Builder(activity)
  .setTitle(init.title)
  .setView(loadlayout({
    LinearLayout;
    orientation="vertical";
    layout_width="fill";
    {
      EditText;
      text=init.text or "";
      Hint=init.hint or "";
      singleLine=true;
      Focusable=true;
      id="DiaEdit";
      layout_width="fill";
      layout_marginLeft="16dp";
      layout_marginRight="16dp";
      layout_marginTop="8dp";
    };
  }))
  .setNegativeButton(AdapLan("取消","Cancel"),{onClick=init.onClick.NegativeButton or function()end})
  .setPositiveButton(AdapLan("确定","OK"),{onClick=init.onClick.PositiveButton or function()end})
  .show()
  _ENV[init.id.Edit or "DiaEdit"]=DiaEdit
end
]]


function MyDialog.EditDialog(...)
  local _dia={}
  _dia.AlertDialog=AlertDialog.Builder(...)
  _dia.MainView=loadlayout({
    LinearLayout;
    orientation="vertical";
    layout_width="fill";
    Focusable=true;
    {
      EditText;
      --singleLine=true;
      inputType="text";
      lines=1;
      layout_width="fill";
      layout_marginLeft="24dp";
      layout_marginRight="24dp";
      layout_marginTop="8dp";
    };
  })
  _dia.AlertDialog.setView(_dia.MainView)
  _dia.EditText=_dia.MainView.getChildAt(0)
  local oldDialogApi={"setIcon","setTitle","setMessage","setPositiveButton","setNeutralButton","setNegativeButton","setView","setCancelable"}
  for index,content in pairs(oldDialogApi) do
    _dia[content]=function(...)
      _dia.AlertDialog[content](...)
      return _dia
    end
  end
  local oldEditTextApi={"setText","setHint"}
  for index,content in pairs(oldEditTextApi) do
    _dia[content]=function(...)
      _dia.EditText[content](...)
      return _dia
    end
  end

  _dia.getText=function()
    return _dia.EditText.getText()
  end
  _dia.getHint=function()
    return _dia.EditText.getHint()
  end

  function _dia.getAlertDialog()
    return _dia.AlertDialog
  end
  function _dia.getEditText()
    return _dia.EditText
  end
  function _dia.getShowed()
    return _dia.showed
  end
  function _dia.show(...)
    _dia.showed=_dia.AlertDialog.show(...)
    function _dia.getButton(...) return _dia.showed.getButton(...) end
    _dia.BUTTON_NEUTRAL=_dia.showed.BUTTON_NEUTRAL
    _dia.BUTTON_NEGATIVE=_dia.showed.BUTTON_NEGATIVE
    _dia.BUTTON_POSITIVE=_dia.showed.BUTTON_POSITIVE
    return _dia
  end

  return _dia
end




function MyDialog.SeekBar(...)
  local _dia={}
  _dia.AlertDialog=AlertDialog.Builder(...)
  _dia.MainView=loadlayout({
    LinearLayout;
    gravity="center";
    layout_height="fill";
    layout_width="fill";
    paddingTop="16dp";
    {
      TextView;
      textColor=普通文字色;
      layout_marginLeft="16dp";
      layout_height="48dp";
      layout_width="48dp";
      gravity="center";
      text="0";
      Background=Ripple(nil,普通波纹);
      onClick=function(view)
        local function setHorizontalPopMenu(item)
          _dia.SeekBar.setProgress(tonumber(item.title)-_dia.alwaysAppend)
        end
        local pop=PopupMenu(activity,view)
        menu=pop.Menu
        for index,content in pairs(_dia.popItems) do
          menu.add(tostring(content)).onMenuItemClick=setHorizontalPopMenu
        end
        pop.show()
      end;
    };
    {
      SeekBar;
      layout_weight="1";
      layout_marginRight="16dp";
    };
  })
  _dia.AlertDialog.setView(_dia.MainView)
  _dia.TextView=_dia.MainView.getChildAt(0)
  _dia.SeekBar=_dia.MainView.getChildAt(1)
  local oldDialogApi={"setIcon","setTitle","setMessage","setPositiveButton","setNeutralButton","setNegativeButton","setView"}
  for index,content in pairs(oldDialogApi) do
    _dia[content]=function(...)
      _dia.AlertDialog[content](...)
      return _dia
    end
  end

  local oldSeekBarApi={"setMax","getProgress","setProgress"}
  for index,content in pairs(oldSeekBarApi) do
    _dia[content]=function(...)
      _dia.SeekBar[content](...)
      return _dia
    end
  end

  _dia.SeekBar.setOnSeekBarChangeListener{
    onProgressChanged=function(SeekBar,progress)
      _dia.TextView.text=tostring(progress+_dia.alwaysAppend)
    end}

  _dia.popItems={}
  _dia.getText=_dia.TextView.getText

  function _dia.getAlertDialog()
    return _dia.AlertDialog
  end
  function _dia.getSeekBar()
    return _dia.SeekBar
  end
  function _dia.setPopItems(t)
    _dia.popItems=t
    return _dia
  end
  _dia.alwaysAppend=0
  function _dia.setAlwaysAppend(number)
    _dia.alwaysAppend=number
    return _dia
  end

  function _dia.getTextView()
    return _dia.TextView
  end
  function _dia.getShowed()
    return _dia.showed
  end
  function _dia.show(...)
    _dia.showed=_dia.AlertDialog.show(...)
    function _dia.getButton(...) return _dia.showed.getButton(...) end
    _dia.BUTTON_NEUTRAL=_dia.showed.BUTTON_NEUTRAL
    _dia.BUTTON_NEGATIVE=_dia.showed.BUTTON_NEGATIVE
    _dia.BUTTON_POSITIVE=_dia.showed.BUTTON_POSITIVE
    return _dia
  end

  return _dia
end



return MyDialog